package com.glorysoft.wangnianxinpda.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.widget.Toast;

import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_InitUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;

/**
 * @Author Zhangjy
 * @Date 2021/6/28 13:45
 * Description :
 */
public class App extends Application {
    public static Context app = null;
    public static String IMEI_ID = "";
    public App instance = null;

    @SuppressLint("HardwareIds")
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        app = getApplicationContext();

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
//                TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
//                IMEI_ID = telephonyManager.getDeviceId();
//                if (IMEI_ID == null) {
//                    IMEI_ID = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
//                }
//            } else {
//                //
//
//            }
//        } else {
//            TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
//            IMEI_ID = telephonyManager.getDeviceId();
//            if (IMEI_ID == null) {
//                IMEI_ID = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
//            }
//        }
        IMEI_ID = getMacAddress();
        FreeApi_StaticMembers.CURRENT_THEME = FreeApi_StaticMembers.THEME_GLORY;
        FreeUI_InitUtils.init(getApplicationContext(), this, false, 60 * 1000L, 0);
        //Toast显示时长
        FreeApi_StaticMembers.TOASTSHOWTIME = Toast.LENGTH_LONG;

        String timeOut = FreeApi_SharePreferencesUtils.getSharePre(
                "TIME_OUT_FILE",
                "TIME_OUT2",
                getApplicationContext()
        );
        if (!TextUtils.isEmpty(timeOut)) {
            Constant.DEFAULT_TIME_OUT = timeOut;
        } else {
            FreeApi_SharePreferencesUtils.setSharePre(
                    "TIME_OUT_FILE",
                    "TIME_OUT2",
                    Constant.DEFAULT_TIME_OUT,
                    getApplicationContext()
            );
        }

        FreeUI_InitUtils.init(
                getApplicationContext(),
                this,
                false,
                Integer.parseInt(Constant.DEFAULT_TIME_OUT) * 1000L,
                0
        );
//        FreeUI_InitUtils.setTheme(FreeApi_StaticMembers.THEME_GLORY);
        String ip = FreeApi_SharePreferencesUtils.getSharePre("URL_FILE", "IP_URL1", getApplicationContext());
        if (!TextUtils.isEmpty(ip)) {
            Constant.NET_URL = ip;
        } else {
            FreeApi_SharePreferencesUtils.setSharePre(
                    "URL_FILE",
                    "IP_URL1",
                    Constant.NET_URL,
                    getApplicationContext()
            );
        }
        String fileIP =
                FreeApi_SharePreferencesUtils.getSharePre("URL_FILE", "FILE_IP", getApplicationContext());
        if (fileIP.length() > 0) {
            Constant.FILE_URL = fileIP;
        } else {
            FreeApi_SharePreferencesUtils.setSharePre(
                    "URL_FILE",
                    "FILE_IP",
                    Constant.FILE_URL,
                    getApplicationContext()
            );
        }

//        String IMEID =
//                FreeApi_SharePreferencesUtils.getSharePre("IMEID", "IMEID", getApplicationContext());
//        if (IMEID.length() > 0) {
//            Constant.IMEI_ID = IMEID;
//        } else {
//            FreeApi_SharePreferencesUtils.setSharePre(
//                    "IMEID",
//                    "IMEID",
//                    Constant.IMEI_ID,
//                    getApplicationContext()
//            );
//        }
        Constant.IMEI_ID = IMEI_ID;
    }

    private String getMacAddress(){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
            return getMacDefault();
        }else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
            return getMacAddressM();
        }

        return getMacFromHardware();

    }

    private String getMacDefault(){
        String address = "未获取到MAC地址";
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        String macAddress = connectionInfo.getMacAddress();
        address = macAddress.toUpperCase(Locale.ENGLISH);
        return address;
    }

    private String getMacAddressM(){
        String address = "未获取到MAC地址";
        try {
            address = new BufferedReader(new FileReader("/sys/class/net/wlan0/address")).readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return address;
    }

    private String getMacFromHardware(){
        try {
            ArrayList<NetworkInterface> list = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface nif : list) {
                if (!"wlan0".equalsIgnoreCase(nif.getName())) {
                    continue;
                }
                byte[] macBytes = nif.getHardwareAddress();
                if (macBytes == null || macBytes.length == 0) {
                    continue;
                }
                StringBuilder result = new StringBuilder();
                for (byte b : macBytes) {
                    result.append(String.format("%02X", b));
                }
                return result.toString().toUpperCase();

            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "未获取到MAC地址";
    }
}

